//
//  UIWebvc.m
//  testAAA
//
//  Created by tianNanYiHao on 2019/11/1.
//  Copyright © 2019 tianNanYiHao. All rights reserved.
//

#import "OldJS2OCvc.h"
#import "XWFT.h"

@interface OldJS2OCvc ()<UIWebViewDelegate>
@property (strong,nonatomic) UIWebView *web;
@property (nonatomic, strong) UIButton* btn1;
@property (nonatomic, strong) UIButton* btn2;
@property (nonatomic, assign) CGFloat halfHeight;
@property (nonatomic, strong ) UILabel *lab;
@end

@implementation OldJS2OCvc

//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e
//https://www.jianshu.com/p/fad8c7844d3e




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor grayColor];
    self.halfHeight = [UIScreen mainScreen].bounds.size.height/2;
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 500)];
    self.web.delegate = self;
    
    
    // 加载本地模拟html
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Test4" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSString *basePath = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:basePath];
    [self.web loadHTMLString:htmlString baseURL:baseURL];
    [self.view addSubview:self.web];

}


- (void)btnClick{
    //首先创建JSContext 对象（此处通过当前webView的键获取到jscontext） 相当于js环境中的 window
    JSContext *context = [self.web valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    // 1.1 js如何注入?
    // 可以通过 context上下文 直接注入当前web的js代码, 在 html中, js直接执行getNum(10) 或 getNum(a) 方法  => js调用/oc提供实现方法
    [context evaluateScript:@"var a = 10"];
    [context evaluateScript:@"var getNum = function(param){return param*3}"];
    
    JSValue *va = [context evaluateScript:@"getNum(3)"];
    self.lab.text = va.toString;
    
}
-(void)btnClick2{
    
    //首先创建JSContext 对象（此处通过当前webView的键获取到jscontext） 相当于js环境中的 window
    JSContext *context = [self.web valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    JSValue *va = [context evaluateScript:@"jsFunc()"];
    self.lab.text = va.toString;
}


-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    //首先创建JSContext 对象（此处通过当前webView的键获取到jscontext） 相当于js环境中的 window
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    // 2.1 JSExport 协议方式, 实现
    // 创建一个遵循 JSExport的 XWFT类, 挂载到js的全局window中
    XWFT *xwft = [[XWFT alloc] init];
    context[@"xwft"] = xwft;
    
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    
    
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//
//     2.2 如果不想通过 JSExport 的方式进行处理
//     可以如下操作, 通过传递 oc的Block,
        [context evaluateScript:@"var xwft2 = {}"]; // 向js 注入一个 xwft 对象
    
        context[@"xwft2"][@"getLocation"] = ^(){
            NSArray *args =[JSContext currentArguments];
            NSLog(@"%@",args);
            JSValue *cb = [args[1] callWithArguments:@[@"sb"]];
        };
    
}

- (void)viewDidAppear:(BOOL)animated{
    
    
    
}



@end
